package cn.uin.trigger.http;

import cn.uin.api.IAuthService;
import cn.uin.api.dto.request.auth.EditUserDetailDTO;
import cn.uin.api.dto.request.auth.RegisterRequestDTO;
import cn.uin.api.dto.request.auth.SendEmailRequestDTO;
import cn.uin.api.dto.request.auth.SendSMSRequestDTO;
import cn.uin.api.dto.response.auth.RegisterResponseDTO;
import cn.uin.api.response.Response;
import cn.uin.domain.auth.model.entity.HomeUserDetailEntity;
import cn.uin.domain.auth.model.entity.HomeUserEntity;
import cn.uin.domain.auth.model.entity.HomeUserRegisterEntity;
import cn.uin.domain.auth.service.IHomeUserService;
import cn.uin.types.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @ClassName AuthController
 * @Description:认证服务接口
 * @Author: Administrator
 * @CreateDate: 2025/10/20 9:32
 * @Version: 1.0
 */
@Slf4j
@RestController()
@CrossOrigin("*")
@RequestMapping("/api/auth/")
public class AuthController implements IAuthService {

	@Resource
	private IHomeUserService iHomeUserService;

	/**
	 * 用户注册
	 * @param registerRequestDTO
	 * @return
	 */
	@Override
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public Response<RegisterResponseDTO> register(@RequestBody RegisterRequestDTO registerRequestDTO) {
		// 构建请求参数
		HomeUserRegisterEntity homeUserRegister = HomeUserRegisterEntity.builder().build();
		BeanUtils.copyProperties(registerRequestDTO, homeUserRegister);
		// 注册用户
		HomeUserEntity homeUserEntity = iHomeUserService.register(homeUserRegister);
		return Response.<RegisterResponseDTO>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(RegisterResponseDTO.builder()
						.username(homeUserEntity.getUsername())
						.password(homeUserEntity.getPassword())
						.email(homeUserEntity.getEmail())
						.phone(homeUserEntity.getPhone())
						.build())
				.build();
	}

	/**
	 * 发送邮箱验证码
	 * @param sendEmailRequestDTO
	 * @return
	 */
	@Override
	@RequestMapping(value = "sendEmailCode", method = RequestMethod.POST)
	public Response<Boolean> sendEmailCode(@RequestBody SendEmailRequestDTO sendEmailRequestDTO) {
		boolean sendEmailFlag = iHomeUserService.sendEmailCode(sendEmailRequestDTO.getType(), sendEmailRequestDTO.getToEmail());
		return Response.<Boolean>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(sendEmailFlag)
				.build();
	}

	/**
	 * 发送手机验证码
	 * @param sendSMSRequestDTO
	 * @return
	 */
	@Override
	@RequestMapping(value = "sendPhoneCode", method = RequestMethod.POST)
	public Response<Boolean> sendPhoneCode(@RequestBody SendSMSRequestDTO sendSMSRequestDTO) {
		boolean sendEmailFlag = iHomeUserService.sendPhoneCode(sendSMSRequestDTO.getType(), sendSMSRequestDTO.getPhone());
		return Response.<Boolean>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(sendEmailFlag)
				.build();
	}

	/**
	 * 编辑用户详细信息
	 * @param editUserDetailDTO
	 * @return
	 */
	@Override
	@RequestMapping(value = "editUserDetail", method = RequestMethod.POST)
	public Response<Boolean> editUserDetail(@RequestBody EditUserDetailDTO editUserDetailDTO) {
		HomeUserDetailEntity homeUserDetailEntity = HomeUserDetailEntity.builder().build();
		BeanUtils.copyProperties(editUserDetailDTO, homeUserDetailEntity);
		boolean editUserDetailFlag = iHomeUserService.editUserDetail(homeUserDetailEntity);
		return Response.<Boolean>builder()
				.code(ResponseCode.SUCCESS.getCode())
				.info(ResponseCode.SUCCESS.getInfo())
				.data(editUserDetailFlag)
				.build();
	}


	@Override
	@PreAuthorize("hasPermission('user', 'user:test1')")
	@RequestMapping(value = "test", method = RequestMethod.POST)
	public Response<RegisterResponseDTO> test() {
		return Response.<RegisterResponseDTO>builder()
				.build();
	}
}
